//
//  ViewController.m
//  14-UICollectionView
//
//  Created by Snow on 16/8/2.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#define kCollection @"reuse"
#define kReuse @"header"
#define kreuse @"footer"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.UICOllectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //1.itemSize 默认大小50,50 self.view.bounds.size.width / 2
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    //2.scrolldirection 滚动方向改变 会交换列和行
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //3.最小行间距
    layout.minimumLineSpacing = 5;
    //4.最小列间距
    layout.minimumInteritemSpacing = 5;
    //5.sectionInset (上左下右)
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //6.区头大小 横向滚动时 设置宽;纵向滚动时 设置高
    layout.headerReferenceSize = CGSizeMake(100, 100);
    //7.区尾大小 同区头
    layout.footerReferenceSize = CGSizeMake(100, 100);
    //8.区头悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    //9.区尾悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    // UICOllectionView创建
    //1.frame
    //2.填入不能为空的layout布局
    
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollection];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReuse];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kreuse];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollection forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor greenColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  
    UICollectionReusableView *resuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReuse forIndexPath:indexPath];
    resuse.backgroundColor = [UIColor redColor];
        
        return resuse;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *resuse = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kreuse forIndexPath:indexPath];
        resuse.backgroundColor = [UIColor blueColor];
        return resuse;
    }
    return nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
